﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCL.Powersource.ViewModels.Analysis;

namespace UCL.Powersource.Views.Analysis
{
    public abstract class ChartViewBase : ContentControl
    {
        protected StatisticViewModelBase VM { get { return (StatisticViewModelBase)(this.DataContext); } }
        protected WindowsFormsHost Host { get; private set; }
        protected Chart MyChart;

        protected abstract SeriesChartType SeriesType { get; }
        protected abstract string LegendTitle { get; }
        protected abstract void FillChart();

        protected DataTable Table
        {
            get
            {
                if (VM == null || VM.StatisticResultsTable == null) return null;

                return VM.StatisticResultsTable;
            }
        }

        public ChartViewBase()
        {

            this.Loaded += ChartViewBase_Loaded;
            this.Unloaded += ChartViewBase_Unloaded;

        }

        void ChartViewBase_Unloaded(object sender, RoutedEventArgs e)
        {
            if (VM != null) VM.Deactivate();
        }

        void ChartViewBase_Loaded(object sender, RoutedEventArgs e)
        {
            if (Host == null)
            {
                Host = new WindowsFormsHost();
                Host.ClipToBounds = true;
                Host.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                Host.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                Host.Visibility = System.Windows.Visibility.Visible;
                this.Content = Host;

                MyChart = new Chart();
                Host.Child = MyChart;

                var leg = new Legend();
                leg.Title = LegendTitle;
                leg.Enabled = true;
                MyChart.Legends.Add(leg);

                MyChart.ChartAreas.Add(new ChartArea());
                //MyChart.ChartAreas[0].AxisX.IsMarginVisible = false;
                MyChart.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
                MyChart.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;
            }

            if (VM != null)
            {

                VM.PropertyChanged -= VM_PropertyChanged;
                VM.PropertyChanged += VM_PropertyChanged;
                VM.Activate();

                RecalculateChart();
            }
        }

        private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (Helpers.PropertyExtensions.IsPropertyChanged(e, () => VM.StatisticResultsTable))
            {
                RecalculateChart();
            }
        }

        private void RecalculateChart()
        {
            if (MyChart.DataSource == Table) return;
            ClearChart();
            if (string.IsNullOrWhiteSpace(Table.TableName)) return;
            FillChart();
            MyChart.DataBind();
        }

        protected virtual void ClearChart()
        {
            MyChart.DataSource = Table;
            MyChart.Series.Clear();
            MyChart.Titles.Clear();
        }

        protected virtual Series GenerateSeries(string Title, string XValueMember, string YValueMembers)
        {
            var newSer = new Series(Title);

            newSer.XValueMember = XValueMember;
            newSer.YValueMembers = YValueMembers;
            newSer.ChartType = SeriesType;

            if (SeriesType == SeriesChartType.Line)
            {
                newSer.MarkerStyle = MarkerStyle.Circle;
                newSer["PointWidth"] = "0.8";
                newSer.BorderWidth = 2;
            }

            if (SeriesType == SeriesChartType.Bar)
            {
                newSer.IsValueShownAsLabel = true;
                newSer["DrawingStyle"] = "Emboss";
            }

            return newSer;
        }
    }
}
